#include <iostream>
#include <pthread.h>

int counter = 0;
pthread_mutex_t m_mutex = PTHREAD_MUTEX_INITIALIZER;

void* print_message_function( void* ptr )
{
	char *message;
	message = (char*)ptr;
	std::cout << message << std::endl;
	pthread_mutex_lock (&m_mutex);
	counter++;
	std::cout << counter << std::endl;
	//pthread_mutex_unlock (&m_mutex);
}

int main()
{
	pthread_t thread1, thread2, thread3;
	char *message1 = (char*)"Thread 1";
	char *message2 = (char*)"Thread 2";
	char *message3 = (char*)"Thread 3";
	int iret1, iret2;
	/* Create independent threads each of which will execute function */
	pthread_create( &thread1, NULL, &print_message_function, (void*) message1);
	pthread_create( &thread2, NULL, &print_message_function, (void*) message2);
	pthread_create( &thread3, NULL, &print_message_function, (void*) message3);

	/* Wait till threads are complete before main continues. Unless we
	* wait we run the risk of executing an exit which will terminate
	* the process and all threads before the threads have completed.
	*/
	
	pthread_join( thread1, NULL);
	pthread_join( thread2, NULL);
	pthread_join( thread3, NULL);
	return 0;
}
